﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.Threading;


namespace MultiKinectSln
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private KinectSensor _nui1 = null;
        private KinectSensor _nui2 = null;

        private Thread _threadKinect1 = null;
        private Thread _threadKinect2 = null;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (KinectSensor.KinectSensors.Count <= 0)
            {
                return;
            }
            _threadKinect1 = new Thread(new ThreadStart(Kinect1Start)); // 开两个线程单独处理
            _threadKinect1.Start();
            if (KinectSensor.KinectSensors.Count >= 2 )
            {
                _threadKinect2 = new Thread(new ThreadStart(Kinect2Start));
                _threadKinect2.Start();
            }

        }
        /// <summary>
        /// 线程1的处理
        /// </summary>
        private void Kinect1Start()
        {
            while (true)
            {
                _nui1 = KinectSensor.KinectSensors[0];
                if (_nui1.Status == KinectStatus.Connected)
                {
                    _nui1.ColorStream.Enable();
                    _nui1.DepthStream.Enable();
                    _nui1.SkeletonStream.Enable();
                    _nui1.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_nui1_AllFramesReady);
                    _nui1.Start();
                    break;
                }
            }
        }
        /// <summary>
        /// 线程2的处理
        /// </summary>
        private void Kinect2Start()
        {
            while (true)
            {
                _nui2 = KinectSensor.KinectSensors[1];
                if (_nui2.Status == KinectStatus.Connected)
                {
                    _nui2.ColorStream.Enable();
                    _nui2.DepthStream.Enable();
                    _nui2.SkeletonStream.Enable();
                    _nui2.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_nui2_AllFramesReady);
                    _nui2.Start();
                    break;
                }
            }
        }

        void _nui1_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            // throw new NotImplementedException();
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }
                byte[] pixels = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(pixels);
                int stride = colorFrame.Width * 4;
                img_color1.Dispatcher.Invoke(new Action(() =>
                { // Dispatcher.Invoke 方法，只有在其上创建 Dispatcher 的线程才可以直接访问 DispatcherObject。
                    img_color1.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                        96, 96, PixelFormats.Bgr32, null, pixels, stride);
                }));
                
            }
        }
        void _nui2_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            // throw new NotImplementedException();
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }
                byte[] pixels = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(pixels);
                int stride = colorFrame.Width * 4;
                img_color2.Dispatcher.Invoke(new Action(() =>
                {
                    img_color2.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                    96, 96, PixelFormats.Bgr32, null, pixels, stride);
                }));
                
            }
        }
        void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                sensor.Stop();
                if (sensor.AudioSource != null)
                {
                    sensor.AudioSource.Stop();
                }
            }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
            _threadKinect1.Abort();
            _threadKinect2.Abort();
            StopKinect(_nui1);
            StopKinect(_nui2);
            
        }
    }
}